<?php
  /*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * @package    symfony
 * @subpackage log
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfFileLogger.class.php 3329 2007-01-23 08:29:34Z fabien $
 */
class KernelLog extends sfFileLogger
{
    protected $php2sfCode = array(
        E_ERROR             => SF_LOG_ERR    ,
        E_WARNING           => SF_LOG_WARNING,
        E_PARSE             => SF_LOG_EMERG  ,
        E_NOTICE            => SF_LOG_NOTICE ,
        E_CORE_ERROR        => SF_LOG_ERR    ,
        E_CORE_WARNING      => SF_LOG_WARNING,
        E_COMPILE_ERROR     => SF_LOG_ERR    ,
        E_COMPILE_WARNING   => SF_LOG_WARNING,
        E_USER_ERROR        => SF_LOG_ERR    ,
        E_USER_WARNING      => SF_LOG_WARNING,
        E_USER_NOTICE       => SF_LOG_NOTICE ,
        E_STRICT            => SF_LOG_NOTICE ,
        E_RECOVERABLE_ERROR => SF_LOG_EMERG  ,
    );
    
   /**
   * Inicializa el  file logger.
   *
   * @param array Options for the logger
   */
  public function initialize($options = array())
  {
      if(!isset($options['php_level']))$options['php_level']= E_ERROR;
      set_error_handler(array($this, 'error_handler'), $options['php_level']);
      sfMixer::register('sfException:printStackTrace',  array($this, 'exception_handler')); 
      parent::initialize($options);
  }

  public function log($message, $priority, $priorityName)
  {
      
    $line = sprintf("%s %s [%s] %s%s", strftime('%b %d %H:%M:%S'), 'symfony', $priorityName, $message, DIRECTORY_SEPARATOR == '\\' ? "\r\n" : "\n");
    
    $lineBD = sprintf("%s%s", $message, DIRECTORY_SEPARATOR == '\\' ? "\r\n" : "\n"); 
       // Propel::close();
   
    if(in_array($priorityName, array('emerg', 'alert', 'crit', 'err') ) )
    {
        if(Kernel::getUser() && $lineBD != '' && $lineBD != null)
        {
            try
            {
                
                $con =     Kernel::getContexto()->getDatabaseConnection('propel');
                $errorapli = new Errordesqldelaaplicacion();
                
                $errorapli->setErrGtaNombre(Kernel::getGrupoTrabajo());
                $errorapli->setErrIp(Kernel::getIP());
                $errorapli->setErrMensaje($lineBD);
                $errorapli->setErrFechaOcurrencia(time());
                $errorapli->setErrAccionHttp(Kernel::getAccion());
                $errorapli->setErrHttpPostVars(str_replace("\n", '', var_export(Kernel::getRequestParameters(), true)));
                $errorapli->setErrUsuario(Kernel::getLogin());
                
                if($con !== null)
                {
                    $errorapli->save($con);
                }    
            
            }catch(Exception $e)
            {
                echo "<br>".$e->getMessage();
            }
        }
    }
    
        
    

    flock($this->fp, LOCK_EX);
    fwrite($this->fp, $line);
    flock($this->fp, LOCK_UN);
  }
  
    /**
     * Funcion para manipular los errores generados para php 
     *
     * @param integer Codigo de error
     * @param string  Mensaje
     * @param string  Archivo
     * @param integer Linea
     */
    public function error_handler ($errno, $errstr, $errfile, $errline) 
    {
        sfLogger::getInstance()->log(
            "{PHP} $errstr en $errfile linea $errline",
            (isset($this->php2sfCode[$errno])?$this->php2sfCode[$errno]:SF_LOG_ALERT)
        );
    }
    
    /**
     * Funcion para manipular las excepciones generadas para php
     *
     * @param Exception The uncaughted exception
     */
    public function exception_handler ($exc1, $exc2 = null) 
    {
        $exc = (empty($exc2)?$exc1:$exc2);
        sfLogger::getInstance()->log(
            "{Exception} ".
            $exc->getMessage(). " en ". $exc->getFile(). " linea ".  $exc->getLine(),  SF_LOG_ALERT
        );
    }


}
?>
